package com.shenmazong.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author 阿瑞
 * @version 1.0
 * @description TODO
 * @class DayNumConfig ???
 * @date 2021/9/27 8:28
 */
@Component
public class DayNumConfig {

    public static final String NUM_EXCHANGE="num_exchange";

    public static final String NUM_QUEUE="num_queue";

    public static final String NUM_DEAD_EXCHANGE="num_dead_exchange";

    public static final String NUM_DEAD_QUEUE="num_dead_queue";

    public static final String NUM_DEAD_KEY="num_dead_key";

    @Bean("numDeadQueue")
    public Queue numDeadQueue(){
        return new Queue(NUM_DEAD_QUEUE);
    }
    @Bean("numDeadExchange")
    public DirectExchange numDeadExchange(){
        return new DirectExchange(NUM_DEAD_EXCHANGE);
    }
    @Bean
    public Binding deadBinding(@Qualifier("numDeadQueue") Queue queue,@Qualifier("numDeadExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with(NUM_DEAD_KEY);
    }

    @Bean("numQueue")
    public Queue numQueue(){
        HashMap<String, Object> map = new HashMap<>();
        //1.设置过期时间
        map.put("x-message-ttl",1000*60*60*24);
        //2.设置死信exchange
        map.put("x-dead-letter-exchange",NUM_DEAD_EXCHANGE);
        //3.设置死信key
        map.put("x-dead-letter-routing-key",NUM_DEAD_KEY);
        return QueueBuilder.durable(NUM_QUEUE).withArguments(map).build();
    }
    @Bean("numExchange")
    public FanoutExchange numExchange(){
        return new FanoutExchange(NUM_EXCHANGE);
    }
    @Bean
    public Binding numBinding(@Qualifier("numQueue") Queue queue,@Qualifier("numExchange") FanoutExchange fanoutExchange){
        return  BindingBuilder.bind(queue).to(fanoutExchange);
    }
}
